//
//  NetworkManager.m
//  mhomeapp
//
//  Created by 刘春雷 on 16/8/25.
//  Copyright © 2016年 刘春雷. All rights reserved.
//

#import "NetworkManager.h"

@implementation NetworkManager

+ (NetworkManager *)manager {
    
    static dispatch_once_t once;
    static NetworkManager *manager;
    dispatch_once(&once, ^{

        manager = [[self alloc] init];
    });
//    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//    NSUserDefaults *de = [NSUserDefaults standardUserDefaults];
    [manager.requestSerializer setValue:@"qS1zD5wN5SNRp8vG0gW3yfqZ1DhIczTgoinCsbnLOoDa0dCXU9bcuQ6NpF2c/hnQSgwyvGNSXvTHsh8UhBpiJGtHbaxaclIkE1chdPIInyY24BR8z1LbAQA/t9LcVn5zuffCOftggodX+IL1+ZIItASetcLt/tYZotCRteX8JNFsX+WjWLXOg6XZZszyRbqnKNk83fMQ+JWJ4aAVx2c3bbQI6ngl2/+JuiwC+qsuHIQWMSH8Ut9ZgK6VwhK9NgIdivrDzc91qTAfUQh3bmDhRaR7yjnBstNY4cmcNJ8kBYQNlwd5DiaW0x+/95QLrJ6u2LI+d9NU5jJlZZOfz6ZHtL2Z3cbkzh4Pr7+wX5pmIN5fYQZ4SKjnS2KEIZLTdsWHnn9Ix66MLi/4+FcOOGNVrzcEezCsRmK629ZoPn+HX8XYMX3V2ar1IjcNlCFY4gnP2BLNKPvtA8Dcr1znzDUL71qou6NIKJ5xw9qpVazDNWmlLfxQqtcVQzlRgkBCqvl5SXLNDzyI4YFJYI+FU6+NKMFD+BI/LGU4Y4q2pAmmgzIftw6gUmYqVHiozbIlOsfppKSikYpPkmU88xWCfR4tEOdRpGo/2l9Ftm7bKP2lH6aW5DG0mkCrJJ+KpF4JicwrhCFK0iNyuB+/d5cmsYth1Dk/XzlUVorKRl5dUP7fbW07dUyh1ATzbIrRuWuqNgZV+qQPahU57rp7+W2QRrKhIdEPSRyOoonw6jq/xNPFfUdz1JtBGycsZKxj1ECKfOoYM2qu+lN6nzfoFhAGuxaSryu2pNC18//oWPJDT3l6WvqGHR9f1dRfexJuHakQj2vyVI3qrBsX0cG5X90nJvUDraRjuxlVU/aJGwPGu+i82UGSVmw1JJjt8A==" forHTTPHeaderField:@"X-Content"];
//    [manager.requestSerializer setValue:[de stringForKey:@"appkey"] forHTTPHeaderField:@"appkey"];
    return manager;
}

- (instancetype)init {
    
    self = [super init];
    return self;
}
- (void)sendGetRequestWithUrl:(NSString *)urlStr paramenters:(NSDictionary *)paramDic success:(successBlock)success failure:(failureBlock)failure {
    
    [self GET:urlStr parameters:paramDic success:^(NSURLSessionDataTask *_Nonnull task, id _Nonnull responseObject) {
        NSDictionary *responseObjectDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        success(responseObjectDic);
    }
      failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {
          failure(error);
          
      }];
    
}

- (void)sendPostRequestWithUrl:(NSString *)urlStr paramenters:(NSDictionary *)paramDic success:(successBlock)success failure:(failureBlock)failure {
    
    NSUserDefaults *de = [NSUserDefaults standardUserDefaults];
    [self.requestSerializer setValue:[de stringForKey:@"userid"] forHTTPHeaderField:@"userId"];
    [self.requestSerializer setValue:[de stringForKey:@"appkey"] forHTTPHeaderField:@"appkey"];
    [self POST:urlStr parameters:paramDic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *responseObjectDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        success(responseObjectDic);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];

}

- (void)sendPutRequestWithUrl:(NSString *)urlStr paramenters:(NSDictionary *)paramDic success:(successBlock)success failure:(failureBlock)failure {
    
    [self PUT:urlStr parameters:paramDic success:^(NSURLSessionDataTask *_Nonnull task, id _Nullable responseObject) {
        
        NSDictionary *responseObjectDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        success(responseObjectDic);
    }
      failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {
          failure(error);
      }];
}

- (void)sendDeleteRequestWithUrl:(NSString *)urlStr paramenters:(NSDictionary *)paramDic success:(successBlock)success failure:(failureBlock)failure {
    
    [self DELETE:urlStr parameters:paramDic success:^(NSURLSessionDataTask *_Nonnull task, id _Nullable responseObject) {
        
        NSDictionary *responseObjectDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        success(responseObjectDic);
    }
         failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {
             failure(error);
         }];
}

@end
